package com.wbao.dianniu.db;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import android.content.Context;

public class DatabaseTools {
	public DatabaseTools() {

	}

	/**
	 * 拷贝空数据库到指定路径
	 * 
	 * @param databasePath
	 *            目标数据路径
	 * @param path
	 *            源路径：raw目录下的数据相对路径 格式如："/raw/database.db"
	 * @throws IOException
	 */
	public void copyDatabase(Context context, String databasePath, int path)
			throws IOException {

		// 数据库文件不存在 直接创建
		File rootfile = new File(databasePath);

		if (!rootfile.exists()) {
			rootfile.createNewFile();
		}
		try {
			InputStream inputstream = context.getResources().openRawResource(path);
			OutputStream outputstream = new FileOutputStream(databasePath);
			byte bt[] = new byte[1024];
			int c;
			while ((c = inputstream.read(bt)) > 0) {
				outputstream.write(bt, 0, c);
			}
			inputstream.close();
			outputstream.close();

		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
	}
}
